package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int queryLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Long count = 0L;
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(friendId).and("likeUserId").is(userId).and("isLike").is(true));
            if(mongoTemplate.count(query1, UserLike.class) == 0) {
                count++;
            }
        }
        return Math.toIntExact(count);
    }

    @Override
    public int queryFanCount(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "userId", Long.class);
        Long count = 0L;
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(friendId).and("isLike").is(false));
            if(mongoTemplate.count(query1, UserLike.class) > 0) {
                count++;
            }
        }
        return Math.toIntExact(count);
    }

    @Override
    public int queryEachLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
//        List<Long> friendIds = mongoTemplate.find(query, Long.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Long count = 0L;
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(friendId).and("likeUserId").is(userId).and("isLike").is(true));
            if(mongoTemplate.count(query1, UserLike.class) > 0) {
                count++;
            }
        }
        return Math.toIntExact(count);
    }

    @Override
    public List<Long> queryEachLike(int page, int pagesize,Long userId) {
        List<Long> userIds = CollUtil.newArrayList();
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true)).skip((page -1 ) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(friendId).and("likeUserId").is(userId).and("isLike").is(true));
            if(mongoTemplate.count(query1, UserLike.class) > 0) {
                userIds.add(friendId);
            }
        }
        return userIds;
    }

    @Override
    public List<Long> queryLike(int page, int pagesize, Long userId) {
        List<Long> userIds = CollUtil.newArrayList();
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true)).skip((page -1 ) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(friendId).and("likeUserId").is(userId).and("isLike").is(true));
            if(mongoTemplate.count(query1, UserLike.class) == 0) {
                userIds.add(friendId);
            }
        }
        return userIds;
    }

    @Override
    public List<Long> queryFan(int page, int pagesize, Long userId) {
        List<Long> userIds = CollUtil.newArrayList();
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true)).skip((page -1 ) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> friendIds = CollUtil.getFieldValues(userLikes, "userId", Long.class);
        for (Long friendId : friendIds) {
            Query query1 = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(friendId).and("isLike").is(true));
            if(mongoTemplate.count(query1, UserLike.class) == 0) {
                userIds.add(friendId);
            }
        }
        return userIds;
    }
}